package Function;
import java.util.Date;
import java.util.StringTokenizer;
import java.text.SimpleDateFormat;

import javax.swing.JOptionPane;

public class CalculateDate {
	String date;
	public int diffDay = 0;
	public CalculateDate(){}
	public CalculateDate(String date)
	{
		this.date = date;
	}
	public void setDate(String date)
	{
		this.date = date;
	}
	public int DayOfMonth(int Month)
	{
		switch(Month)
		{
			case 1 : return 31;
			case 2 : 
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
				Date currentDate = new Date();
				String temp = sdf.format(currentDate);
				int year = Integer.parseInt(temp);
				year -= 543;
				if(year%4==0)
				{
					return 29;
				}else
				{
					return 28;
				}
			case 3 : return 31;
			case 4 : return 30;
			case 5 : return 31;
			case 6 : return 30;
			case 7 : return 31;
			case 8 : return 31;
			case 9 : return 30;
			case 10 : return 31;
			case 11 : return 30;
			case 12 : return 31;
		}
		return 0;
	}
	
	public int CalculateDay(int Month,int Year)
	{
		switch(Month)
		{
			case 1 : return 31;
			case 2 : 
				Year -= 543;
				if(Year%4==0)
				{
					return 29;
				}else
				{
					return 28;
				}
			case 3 : return 31;
			case 4 : return 30;
			case 5 : return 31;
			case 6 : return 30;
			case 7 : return 31;
			case 8 : return 31;
			case 9 : return 30;
			case 10 : return 31;
			case 11 : return 30;
			case 12 : return 31;
		}
		return 0;
	}
	
	public int CheckDate()
	{
		StringTokenizer st = new StringTokenizer(date,"/");
		int day = Integer.parseInt(st.nextToken());
		int month = Integer.parseInt(st.nextToken());
		int year = Integer.parseInt(st.nextToken());
		
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
		Date currentDate = new Date();
		String reBookDate = sdf.format(currentDate);
		st = new StringTokenizer(reBookDate,"/");
		int reDay = Integer.parseInt(st.nextToken());
		int reMonth = Integer.parseInt(st.nextToken());
		int reYear = Integer.parseInt(st.nextToken());
		
		if(reDay > day && month == reMonth && year == reYear)
		{
			if( (day+7) < reDay ){
				diffDay = reDay - (day+7);
				return diffDay*3;
			}
		}
		else if(month < reMonth && year == reYear)
		{
			int sum = DayOfMonth(month) - day;
			if( sum > 7){
				day += 7;
				diffDay = reDay + (DayOfMonth(month) - day) - 1;
				return diffDay*3;
			}else if(sum < 7)
			{
				day += sum;
				sum = 7 - sum;
				day = sum;
				diffDay = (DayOfMonth(reMonth) - day) - (DayOfMonth(reMonth) - reDay) - 1;
				return diffDay*3;
			}
		}else if(year < reYear)
		{
			int sum = day + 7;
			if( sum > DayOfMonth(month))
			{
				sum += 7;
				sum -= DayOfMonth(month);
			}else
			{
				day += 7;
			}
			int i = 1;
			diffDay = 0;
			while(i<=12)
			{
				if(i == month)
				{
					diffDay += CalculateDay(i,year) - day;
					JOptionPane.showMessageDialog(null, diffDay + "->" + i);
				}
				if(i > month)
				{
					diffDay += CalculateDay(i,year);
					JOptionPane.showMessageDialog(null, diffDay + "->" + i);
				}
				i++;
			}
			i = 1;
			while(i<=12)
			{
				if(i < reMonth)
				{
					diffDay += CalculateDay(i,year);
				}
				if(i == reMonth)
				{
					diffDay += reDay;
				}
				i++;
			}
			diffDay -= 1;
			return diffDay*3;
		}
		return 0;
	}
	
	
	public String EndReBookDate()
	{
		StringTokenizer st = new StringTokenizer(date,"/");
		int day = Integer.parseInt(st.nextToken());
		int month = Integer.parseInt(st.nextToken());
		int year = Integer.parseInt(st.nextToken());
		int sum = day + 7;
		if(sum > DayOfMonth(month))
		{
			sum -= DayOfMonth(month);
			if((month+1) < 10)
			{
				if(sum < 10)
				{
					return "0"+ sum + "/" + ( "0"+(month + 1)) + "/" + year;
				}
				return sum + "/" + ( "0"+(month + 1)) + "/" + year;
			}
			if((month+1)==13)
			{
				year++;
				return "0"+ sum + "/" + "01" + "/" + year;
			}
			return sum + "/" + (month+1) + "/" + year;
		}else if(sum == DayOfMonth(month))
		{
			if(month < 10)
			{
				if(sum < 10)
				{
					return sum + "/" + ( "0"+ month) + "/" + year;
				}
				return sum + "/" + ( "0"+ month) + "/" + year;
			}
			return sum + "/" + (month) + "/" + year;
		}else if(sum < DayOfMonth(month))
		{
			if(sum < 10)
			{
				if(month < 10)
				{			
					return "0"+sum + "/" + ( "0"+ month) + "/" + year;
				}
				return "0" + sum + "/" + month + "/" + year;
			}
			return sum + "/" + month + "/" + year;
		}
		return "";
	}
}
